package com.example.mongo.controller;

import com.example.mongo.Model.bean.User;
import com.example.mongo.Model.dto.ResponseData;
import com.example.mongo.Model.dto.UserCURD;
import com.example.mongo.Model.dto.UserRegisterDto;
import com.example.mongo.service.*;
import com.example.mongo.utils.MD5Util;
import com.example.mongo.utils.RedisUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//后台管理
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    ContentService contentService;
    @Autowired
    ChapterService chapterService;
    @Autowired
    ResponseData responseData;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserService userService;
    @Autowired
    MenuService menuService;
    @Autowired
    ShelfService shelfService;


    @RequestMapping(value = "/getUser", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String selectUser(@RequestBody UserCURD userCURD) {
        User user = null;
        List<User> list = new ArrayList<>();
        switch (userCURD.choiceMethod()) {
            case "id":
                user =  userService.getUserById(userCURD.getId());
                break;
            case "userName":
                user = userService.getUserByUserName(userCURD.getUserName());
                break;
            case "isVIP":
                list = userService.getUsersByIsVIP(userCURD.getIsVIP());
                break;
            default:
        }

        if (null==user){
            responseData.error("400","搜索无结果","");
        }else {
            responseData.success("ok", JSONObject.fromObject(user).toString());
            return responseData.toString();
        }
        if (null==list){
            responseData.error("400","搜索无结果","");
        }else {
            responseData.success("ok", JSONArray.fromObject(list).toString());
        }

        return responseData.toString();
    }





    @ResponseBody
    @RequestMapping(value = "/insertAdmin",produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public String insertAdmin(@RequestBody UserRegisterDto dto) {

        if (null != userService.getUserByUserName(dto.getUserName())) {   //判断用户名是否重复
            responseData.error("400","用户名已占用","");
            return responseData.toString();
        }
        User user = new User();
        user.setUserName(dto.getUserName());
        user.setUserPassword(MD5Util.encrypt(dto.getUserName(), dto.getUserPassword()));
        user.setUserPhone(dto.getUserPhone());

        if (null == userService.insertAdmin(user)){
            responseData.error("400","注册错误，请重试","");
        }else {
            responseData.success("注册成功","");
        }
        return responseData.toString();
    }


    @ResponseBody
    @RequestMapping(value = "/deleteUser",produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public String deleteUser(@RequestParam String id) {
        if (userService.deleteUser(id)){
            responseData.success("删除成功！","");
        }else {
            responseData.error("400","删除失败！","");
        }
        return responseData.toString();
    }








}
